package com.chianghao.rock.system.controller;

import java.util.List;
import java.util.stream.Collectors;

import javax.validation.constraints.NotBlank;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.chianghao.core.mybatis.plugins.Paging;
import com.chianghao.core.web.R;
import com.chianghao.rock.admin.system.role.dto.RoleAddDto;
import com.chianghao.rock.admin.system.role.dto.RoleAndRouteDto;
import com.chianghao.rock.admin.system.role.dto.RoleModifyDto;
import com.chianghao.rock.admin.system.role.vo.RoleVo;
import com.chianghao.rock.admin.system.route.vo.RouteVo;
import com.chianghao.rock.common.SequenceService;
import com.chianghao.rock.system.entity.Role;
import com.chianghao.rock.system.entity.Route;
import com.chianghao.rock.system.service.role.RoleService;
import com.chianghao.rock.system.service.role_scope.RoleScopeService;
import com.rock.microshop.common_api.dto.PageDto;
import com.rock.microshop.common_api.vo.PageVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;


@RestController
@RequestMapping("system/role")
@Api(value="角色",tags={"角色"})
public class RoleController {

    @Autowired
    RoleService roleService;
    
    @Autowired
    RoleScopeService roleScopeService;

    @Autowired
    SequenceService sequenceService;

	@PostMapping("add")
	@ApiOperation("添加角色")
	public R<Boolean> add(@Validated RoleAddDto dto) {
		Role role = toEntity(dto);
		Long id  = sequenceService.getNextId();
		role.setId(id);
		roleService.insert(role);
		return R.getSuccess(true);
	}

    @PostMapping("modify")
    @ApiOperation("修改角色")
    public R<Boolean> modify(@Validated RoleModifyDto dto) {
    	Role role = toEntity(dto);
		roleService.updateById(role,role.getId());
    	return R.getSuccess(true);
	}

    @DeleteMapping("delete")
    @ApiOperation("删除角色")
    @ApiImplicitParams({
    	@ApiImplicitParam(name="id",value="主键",dataType = "String",required=true,paramType="query")
    })
    public R<Boolean> delete(@NotBlank(message="error:code,not.null") Long id) {
    	roleService.deleteById(id);
    	return R.getSuccess(true);
    }

    @GetMapping("detail")
    @ApiOperation("详情")
    @ApiImplicitParams({
    	@ApiImplicitParam(name="id",value="主键",dataType = "String",required=true,paramType="query")
    })
    public R<RoleVo> detail(@NotBlank(message="error:code,not.null") Long id) {
    	Role  bean = roleService.queryOneById(id);
    	return R.getSuccess(toVo(bean));
    }

    @GetMapping("query")
    @ApiOperation("模糊匹配查新")
    @ApiImplicitParams({
    	@ApiImplicitParam(name="name",value="名称",dataType = "String",required=false,paramType="query")
    })
    public R<List<RoleVo>> query(String name) {
    	List<Role> List = roleService.query(name);
    	return R.getSuccess(toListVo(List));
    }

    @GetMapping("page")
    @ApiOperation("分页查询")
    @ApiImplicitParams({
    	@ApiImplicitParam(name="name",value="名称",dataType = "String",required=false,paramType="query")
    })
    public R<PageVo<RoleVo>> queryPageList(String name, PageDto page) {
    	Paging paging  = new Paging(page.getCurrent(),page.getPageSize()); 
    	List<Role> List = roleService.queryPage(name,paging );
    	PageVo<RoleVo> pageVo = new PageVo<RoleVo>();
    	pageVo.setItems(toListVo(List));
    	pageVo.setTotal(paging.getTotalRows());
    	return R.getSuccess(pageVo);
    }
    
    
    //添加角色和路由的对应关系
    @PostMapping("set-route")
    @ApiOperation("绑定角色和路由的关系")
    public R<Boolean> bindRoleAndRoute(@Validated @RequestBody RoleAndRouteDto dto){
    	this.roleScopeService.doBuildRoleRoute(dto.getRoleId(),dto.getRouteIds(),dto.getType());
    	return R.getSuccess(true);
    }
    
    @PostMapping("query-role-route")
    @ApiOperation("查询路由的权限")
    @ApiImplicitParams({
    	@ApiImplicitParam(name="roleId",value="路由ID",dataType = "String",required=true,paramType="query"),
    	@ApiImplicitParam(name="type",value="类型[1路由/2路由按钮/3路由接口]",dataType = "Integer",required=true,paramType="query")
    })
    public R<List<RouteVo>> queryRouteList(Long roleId,Integer type){
    	List<Route> list  = this.roleScopeService.queryRouteList(roleId,type);
    	return R.getSuccess(toRouteVoList(list));
    }
    
    private RouteVo toRouteVo(Route route) {
    	RouteVo vo = new RouteVo();
    	BeanUtils.copyProperties(route, vo);
    	return vo;
    } 
    
    private List<RouteVo> toRouteVoList(List<Route> list) {
    	return list.stream().map(item->toRouteVo(item)).collect(Collectors.toList());
    }
    
    private RoleVo toVo(Role bean) {
    	RoleVo vo = new RoleVo();
    	BeanUtils.copyProperties(bean, vo);
    	return vo;
    }
    
    private List<RoleVo> toListVo(List<Role> list){
    	List<RoleVo> array = list.stream().map(item->toVo(item)).collect(Collectors.toList());
    	return array;
    }

    
    private Role toEntity(RoleAddDto dto) {
    	Role bean = new Role();
    	BeanUtils.copyProperties(dto, bean);
    	return bean;
    }
    
    private Role toEntity(RoleModifyDto dto) {
    	Role bean = new Role();
    	BeanUtils.copyProperties(dto, bean);
    	return bean;
    }

}